package com.spzx.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.domain.CategoryBrand;
import com.spzx.mapper.CategoryBrandMapper;
import com.spzx.service.BrandService;
import com.spzx.service.CategoryBrandService;
import com.spzx.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
    @Autowired
    CategoryService categoryService;
    @Resource
    BrandService brandService;

    @Override
    public List<CategoryBrand> listCategoryBrand(CategoryBrand categoryBrand) {
        Long brandId = categoryBrand.getBrandId();
        Long categoryId = categoryBrand.getCategoryId();
        //分类名
        //根据品牌ID和分类ID查询
        List<CategoryBrand> list = this.list(Wrappers.lambdaQuery(CategoryBrand.class)
                .eq(categoryId != null, CategoryBrand::getCategoryId, categoryId)
                .eq(brandId != null, CategoryBrand::getBrandId, brandId));

            list.forEach(categoryBrand1 -> {
                System.out.println(categoryBrand1);
                Brand brandbyId = brandService.getById(categoryBrand1.getBrandId());
                categoryBrand1.setBrandLogo(brandbyId.getLogo());
                categoryBrand1.setBrandName(brandbyId.getName());
            });

            list.forEach(categoryBrand1 -> {
                categoryBrand1.setCategoryIdList(categoryService.getFatherIdList(categoryBrand1.getCategoryId()));
            });

        list.forEach(categoryBrand1 -> {
            categoryBrand1.setCategoryName(categoryService.getNameById(categoryBrand1.getCategoryId()));
        });
        return list;
    }

    @Override
    public CategoryBrand getCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = this.getById(id);
        Long ids = categoryBrand.getCategoryId();
       /* List<Long> list = new ArrayList<>();
        for (;ids > 0;){
            list.add(ids);
            ids = categoryService.getOne(Wrappers.lambdaQuery(Category.class).eq(Category::getId, ids)).getParentId();
        }*/
        List<Long> fatherIdList = categoryService.getFatherIdList(ids);
        categoryBrand.setCategoryIdList(fatherIdList);
        return categoryBrand;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean insert(CategoryBrand categoryBrand) {
        Long brandId = categoryBrand.getBrandId();
        Long categoryId = categoryBrand.getCategoryId();
        long count = this.count(Wrappers.lambdaQuery(CategoryBrand.class).eq(CategoryBrand::getCategoryId, categoryId).eq(CategoryBrand::getBrandId, brandId));
        System.out.println("----------------------------------" + count);
        if (count > 0) {
            throw new ServiceException("该分类下已经存在该品牌");
        }
        boolean save = this.save(categoryBrand);
        return save;
    }

    @Override
    public boolean updateCategoryBrand(CategoryBrand categoryBrand) {
        Long brandId = categoryBrand.getBrandId();
        Long categoryId = categoryBrand.getCategoryId();
        Long id = categoryBrand.getId();
        long count = this.count(Wrappers.lambdaQuery(CategoryBrand.class).eq(CategoryBrand::getCategoryId, categoryId).eq(CategoryBrand::getBrandId, brandId));
        if (count > 0) {
            throw new ServiceException("已经存在该分类下该品牌");
        }
        return this.updateById(categoryBrand);
    }

    @Override
    public List getByC3Id(Long id) {
        List<CategoryBrand> list = this.list(Wrappers.lambdaQuery(CategoryBrand.class).eq(CategoryBrand::getCategoryId, id));
        List<Brand> collect = list.stream().map(categoryBrand -> {
            Long brandId = categoryBrand.getBrandId();
            return brandService.getById(brandId);
        }).collect(Collectors.toList());
        return collect;
    }
}
